import { useCallback } from 'react';
import type { PcFormProps } from '..';

interface UsePcFormOptions<T> {
  /** 提交回调 */
  onSearch?: (values: T) => Promise<void>;
  /** 重置回调 */
  onReset?: () => void;
}

/**
 * PcForm 的自定义 Hook
 * @template T - 表单数据类型
 */
const usePcForm = <T extends Record<string, any>>(options: UsePcFormOptions<T>) => {
  const { onSearch, onReset } = options;

  // 处理表单提交
  const handleFinish = useCallback(async (values: T) => {
    if (onSearch) {
      // 保留分页参数
      const { current, pageSize } = values as any;
      await onSearch({
        ...values,
        current: current || 1,
        pageSize: pageSize || 10,
      } as T);
    }
    return true;
  }, [onSearch]);

  // 处理表单重置
  const handleReset = useCallback(() => {
    if (onReset) {
      onReset();
    }
  }, [onReset]);

  // 表单配置
  const formConfig: Omit<PcFormProps<T>, 'columns'> = {
    onFinish: handleFinish,
    onReset: handleReset,
  };

  return {
    formConfig,
    handleFinish,
    handleReset,
  };
};

export default usePcForm; 